package state

import (
	"fmt"

	"github.com/go-resty/resty/v2"
	"github.com/xuelang-group/suanpan-appconnector/pkg/common"
)

type MethodReq string

const (
	MethodReqStarting         MethodReq = "starting"
	MethodReqSuccess          MethodReq = "success"
	MethodReqFailed           MethodReq = "failed"
	MethodReqStopped          MethodReq = "stopped"
	MethodReqRunning          MethodReq = "running"
	MethodReqCron             MethodReq = "cron"
	MethodReqWaiting          MethodReq = "waiting"
	MethodReqStopping         MethodReq = "stopping"
	MethodReqDead             MethodReq = "dead"
	MethodReqClear            MethodReq = "clear"
	MethodReqClearNode        MethodReq = "clearNode"
	MethodReqSetNodePing      MethodReq = "setNodePing"
	MethodReqGetNodeStatus    MethodReq = "getNodeStatus"
	MethodReqGetNodePing      MethodReq = "getNodePing"
	MethodReqGetNetworkStatus MethodReq = "getNetworkStatus"
	MethodReqSetNodeStatus    MethodReq = "setNodeStatus"
	MethodReqListNetwork      MethodReq = "listNetwork"
	MethodReqGetAll           MethodReq = "getAll"
)

type ArgsReq struct {
	NetworkId string `json:"networkId,omitempty"` // appId
	NodeId    string `json:"nodeId,omitempty"`
	Status    string `json:"status,omitempty"`
	Expire    int    `json:"expire,omitempty"`
}

type UpdateRunningStateReq struct {
	Method MethodReq `json:"method"`
	Args   ArgsReq   `json:"args"`
	Msg    string    `json:"msg,omitempty"`
}

// TODO: cat set retry counts
func SetRunningState(req UpdateRunningStateReq) error {
	// not use running state logic
	// return nil
	endpoint := getStateServiceEndpoint()

	client := resty.New()
	// client.RetryCount = 2

	//logrus.Errorf("Sending request to endpoint: %s", endpoint)
	//logrus.Errorf("Request body: %+v", req)
	_, err := client.R().
		SetHeader("Content-Type", "application/json").
		SetBody(req).
		Post(endpoint)

	return err
}

func getStateServiceEndpoint() string {
	return fmt.Sprintf("%s/internal/task/status/update", common.GetShSolverServiceEndpoint())
}
